#include <string.h>
#include <stdlib.h>
#include "iobot.h"
#include "syscall.h"

char* bot_specials[9] =
{ // Values are in octal
 //| H border | V Border | Top Left | Top Right | Bottom Left | Bottom Right | LeadIn | LeadOut | Blank
	  "\304",    "\263",     "\332",    "\277",      "\300",       "\331",      "\264",  "\303",   "\000"
};

void iobot_update_output(iobot_t* bot)
{
	//if the iobot is not dirty, then don't bother re-drawing it
	if(!bot->dirty)
		return;

	unsigned int res1, res2, res3, res4, success = 0;//needed for the system calls
	unsigned int scrndev = 0;
	syscall1(SYS_OPEN, "/devices/txt", scrndev, success)
	if(!success)
		return;

	//make sure that the text fits in the iobot
	bot->textwidth = bot->width - 2;
	bot->textheight = bot->height - 2;

	unsigned int draw_x = bot->x + bot->dirty_x;
	unsigned int draw_y = bot->y + bot->dirty_y;
	unsigned int draw_w = bot->x + bot->dirty_x + bot->dirty_width;
	unsigned int draw_h = bot->y + bot->dirty_y + bot->dirty_height;
	char* temp_line = (char*)malloc((sizeof(char) * bot->dirty_width) + 1);
	unsigned int line_equation = 0;

	while(draw_y < draw_h)
	{
		memset(temp_line, 0, (sizeof(char) * bot->dirty_width) + 1);
		while(draw_x < draw_w)
		{
			//check if the current drawing position is part of the border or the iobot title
			if(draw_y == bot->y)
			{//top border, possibly the iobot title or top corners
				if(draw_x == bot->x)
				{
					strcat(temp_line, bot_specials[bot_TOP_LEFT]);
				}
				else if(draw_x == bot->x + bot->width - 1)
				{
					strcat(temp_line, bot_specials[bot_TOP_RIGHT]);
				}
				else if(draw_x == bot->x + 3)
				{
					strcat(temp_line, bot_specials[bot_LEADIN]);
				} 
				else if(draw_x == bot->x + 4 + strlen(bot->name))
				{
					strcat(temp_line, bot_specials[bot_LEADOUT]);
				} 
				else if((draw_x > bot->x + 3) & (draw_x < bot->x + 4 + strlen(bot->name)))
				{//the iobot title
					strncat(temp_line, &bot->name[draw_x - 4 - bot->x], 1);
				} 
				else
				{
					strcat(temp_line, bot_specials[bot_TOP]);
				}
			}
			else if(draw_y == bot->y + bot->height - 1)
			{//bottom border or possibly bottom corners
				if(draw_x == bot->x)
				{
					strcat(temp_line, bot_specials[bot_BOTTOM_LEFT]);
				}
				else if(draw_x == bot->x + bot->width - 1)
				{
					strcat(temp_line, bot_specials[bot_BOTTOM_RIGHT]);
				}
				else
				{
					strcat(temp_line, bot_specials[bot_BOTTOM]);
				}
			}
			else if(draw_x == bot->x)
			{//left border
				strcat(temp_line, bot_specials[bot_LEFT]);
			}
			else if(draw_x == bot->x + bot->width - 1)
			{//right border
				strcat(temp_line, bot_specials[bot_RIGHT]);
			}	
			else if((draw_x > bot->x) & (draw_x <= bot->x + bot->textwidth))
			{
				if((draw_y > bot->y) & (draw_y <= bot->y + bot->textheight))
				{//we are in the text area, so write the text
					line_equation = ((((draw_y - bot->y) + bot->scroll)	- 1) * bot->textwidth) + (draw_x - bot->x - 1);
					if(line_equation < 12288)
						strncat(temp_line, (char*)&bot->botout[line_equation], 1);
					else
						strncat(temp_line, bot_specials[bot_BLANK], 1);
				}
			}
			draw_x++;
		}
		draw_x = bot->x + bot->dirty_x;
		syscall2(SYS_DISPLAY_POINTER, draw_x, draw_y, res1, res2, success)
		syscall4(SYS_WRITE, scrndev, 0, temp_line, strlen(temp_line), res1, res2, res3, res4, success)
		draw_y++;
	}
	if(bot->focused)
		syscall2(SYS_DISPLAY_CURSOR, bot->x + 1 + bot->csr_x, bot->y + 1 + bot->csr_y - bot->scroll, res1, res2, success)
	bot->dirty = 0;

	//syscall2(SYS_DISPLAY_POINTER, bot->dirty_x, bot->dirty_y, res1, res2, success)
	//syscall3(SYS_WRITE, scrndev, "+", 1, res1, res2, res3, success)
	//syscall2(SYS_DISPLAY_POINTER, bot->dirty_x + bot->dirty_width, bot->dirty_y + bot->dirty_height, res1, res2, success)
	//syscall3(SYS_WRITE, scrndev, "x", 1, res1, res2, res3, success)

	free(temp_line);
	syscall1(SYS_CLOSE, scrndev, res1, success)
}
